package com.bkitmobile.kesu.android.connectServer;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import com.bkitmobile.kesu.android.GUI.R;
import com.bkitmobile.kesu.android.GUI.StaticDialogs;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class Request {
	public static final String GET_LIST24 = "Get24hList";
	public static final String GET_LIST_VIEW_INVI = "GetViewInviList";
	public static final String LOGOUT = "LogOut";
	public static final String REMOVE_FRIEND = "RemoveFriend";
	public static final String LOGIN = "Login";
	public static final String GET_MY_PROFILE = "GetMyProfile";
	public static final String SEND_MY_PROFILE = "SendMyProfile";
	public static final String CHANGE_PASSWORD = "ChangePassword";
	public static final String GET_FRIEND_PROFILE = "GetFriendProfile";
	public static final String ALLOW_FOLLOW_ME = "AllowFollowMe";
	public static final String DISALLOW_FOLLOW_ME = "DisallowFollowMe";
	public static final String ALLOW_SEE24 = "AllowSee24";
	public static final String DISALLOW_SEE24 = "DisallowSee24";
	public static final String CHANGE_STATUS = "ChangeStatus";
	public static final String REGISTER = "Register";
	public static final String GETLISTPOSITION = "Get24hPosition";
	public static final String CONFIRM_LOGIN = "ConfirmLogin";
	public static final String GET_LOCATIONS = "GetLocations";
	public static final String GET_PLACE_INFO = "GetPlaceInfo";
	public static final String GET_FILTER_PLACES = "GetFilterPlace";
	public static final String GET_SETTING = "GetSetting";
	public static final String SEARCH = "Search";
	public static final String UPDATE_LIST = "UpdateList";
	private static final int HANDLER_SUCCESS = 0;
	private static final int HANDLER_TIME_OUT = 1;
	private static final int TIME_OUT = 10000;
	private static ProgressDialog pd = null;
	public static void sendAndRecieveData(String[] identities,
			String[] atributies, boolean wait, final Context context,
			final ServiceCallback<String> callback) {
		String result = null;
		final Handler mHandler = new Handler() {
			public void handleMessage(Message msg) {
				if (msg.what == HANDLER_SUCCESS)
				{
					pd.dismiss();
				}
				else if (msg.what == HANDLER_TIME_OUT)
				{
					if (!pd.isShowing()) return;
					else {
						pd.dismiss();
					}
				}
			}
		};
		if (identities.length != atributies.length) {
			result = null;
		} else {
			List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>(
					identities.length);
			for (int i = 0; i < identities.length; i++) {
				nameValuePairs.add(new BasicNameValuePair(identities[i],
						atributies[i]));
			}

			pd = ProgressDialog.show(context, "loading...", "loading...", true,
					false);
			Log.d("loading", "loading...");
			// pd.show();
			//mHandler.sendEmptyMessageDelayed(HANDLER_TIME_OUT, TIME_OUT);
			new ThreadWaitingReceive(nameValuePairs) {
				public void onFailure(Exception ex) {
					callback.onFailure(ex);
					mHandler.sendEmptyMessage(HANDLER_TIME_OUT);
				}

				public void onSuccess(String responseMsg) {
					callback.onSuccess(responseMsg);
					mHandler.sendEmptyMessage(HANDLER_SUCCESS);
					Log.d("resp", responseMsg);
				}
			}.start();
		}
	}
}
